package com.dys.testqueue16;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @className: com.dys.testqueue16.User
 * @description: com.dys.testqueue16
 * @author: pine cone
 * @version: v1.0.0
 * @createTime: 2024/08/12 下午11:34
 */
public class User implements Delayed {
    private int userId;
    private String name;
    private Long endTime;

    public User(int userId, String name, Long endTime) {
        this.userId = userId;
        this.name = name;
        this.endTime = endTime;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getEndTime() {
        return endTime;
    }

    public void setEndTime(Long endTime) {
        this.endTime = endTime;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId=" + userId +
                ", name='" + name + '\'' +
                ", endTime=" + endTime +
                '}';
    }

    // 看剩余时间
    @Override
    public long getDelay(TimeUnit unit) {
        // 计算剩余时间，剩余时间<=0 证明已经到期
        return this.getEndTime() - System.currentTimeMillis();
    }

    // 看谁先被移除
    @Override
    public int compareTo(Delayed o) {
        // 队列中数据，到期时间的比较
        User u = (User) o;
        return this.endTime.compareTo(u.endTime);
    }
}
